const webpack = require('webpack')
const HappyPack = require('happypack')
const os = require('os')
const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length });
// const JavaScriptObfuscator = require('webpack-obfuscator');

module.exports = {
    publicPath: '/',
    lintOnSave: false, // 关闭eslint
    productionSourceMap: false,  //去掉打包的时候生成的map文件
    outputDir: 'dist',//打包后的目录名称
    assetsDir: 'static',//静态资源目录名称

    chainWebpack: config => {
        const jsRule = config.module.rule('js');
        jsRule.uses.clear();
        jsRule.use('happypack/loader?id=babel')
            .loader('happypack/loader?id=babel')
            .end();
        config.plugin('provide').use(webpack.ProvidePlugin, [{
            $: 'jquery',
            jquery: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',

        }]),
        {
            test: /\.jpg|png|gif|bmp|ttf|eot|svg|woff|woff2$/,
            use: [
                {
                    loader: 'url-loader',
                    option: {
                        limit: 16940,
                        fallback: {
                            loader: 'file-loader',
                            options: {
                                name: 'fonts/[name].[hash.8].[ext]'
                            }
                        }
                    }
                }
            ]
        }
    },
    configureWebpack: {
        plugins: [
            new HappyPack({
                id: 'babel',
                loaders: ['babel-loader?cacheDirectory=true'],
                threadPool: happyThreadPool
            }),
            new webpack.DefinePlugin({
                VUE_APP_ENABLE_CUSTOM_MENU: process.env.VUE_APP_ENABLE_CUSTOM_MENU
            }),
            // 新添加的 JavaScriptObfuscator 插件  
            ...(process.env.NODE_ENV === 'production' ? [
                // new JavaScriptObfuscator({
                    // compact: true, // 压缩代码  
                    // controlFlowFlattening: false, // 是否启用控制流扁平化（会降低性能）  
                    // deadCodeInjection: false, // 注入随机死代码块  
                    // debugProtection: false, // 调试保护  
                    // debugProtectionInterval: false, // 调试保护间隔  
                    // disableConsoleOutput: true, // 禁用console输出  
                    // 其他混淆选项...  
                // })
            ] : []),
        ]
    },
    devServer: {
        open: true, // 自动打开浏览器
        host: '0.0.0.0', // 允许外部ip访问
        port: 9099, // 端口
        https: false, // 启用https
        overlay: {
            warnings: true,
            errors: true
        },
    },
    css: {
        loaderOptions: {
            sass: {
                implementation: require('sass'), // This line must in sass option
            },
        }
    }
}